
package samples;
// {{{ imports
import jp.ize.iwp.RuntimeEngine;
import jp.ize.iwp.utils.DebugUtil;
import jp.ize.iwp.utils.SceneManager;
import jp.ize.ux.UXRoot;
import jp.ize.ux.acts.Act;
import jp.ize.ux.acts.VisibilityAct;
import jp.ize.ux.components.UXBasicComponent;
import jp.ize.ux.states.UXBasicState;
import jp.ize.ux.components.UXLabel;
import jp.ize.ux.components.UXItem;
import jp.ize.ux.components.UXSelector;
import jp.ize.ux.components.UXFormData;
import jp.ize.ux.tween.easings.Linear;
import hxjson2.JSON;
// }}}
// {{{ class Selector
class Selector
{
	// {{{ properties

	public var scene:SceneManager;
	public var s:UXBasicComponent;
	public var va:VisibilityAct;
	public var fadeparam:FadeParameter;
	public var formdata:UXFormData;
	public var selectors:Array<UXSelector>;

	// }}}
	// {{{ public static function main() : Void

	public static function main() : Void
	{
		RuntimeEngine.init();
		DebugUtil.init();
		DebugUtil.setLocation( 300, 200 );

		var selector = new Selector();
	}

	// }}}
	// {{{ public function new() : Void

	public function new() : Void
	{
		formdata = new UXFormData(getData);
		selectors = new Array();
		va = new VisibilityAct();
		fadeparam = {
			time: 0.6,
			transition: Linear.easeOut,
			dir: VisibilityActDir.HIDE
		}
		init();
	}

	// }}}
	// {{{ public function init() : Void

	public function init() : Void
	{
		var jsonobj = {
			scenes: [
				{
					q:"アンケートをはじめますか？",
					a:["はい","もちろん"]
				},
				{
					q:"今日はどんな乗り物で来ましたか？",
					a:["電車","車"]
				},
				{
					q:"今日の天気は？",
					a:["晴れ","曇り","雨","雪"]
				},
				{
					q:"最後に、今日の気分は？",
					a:["楽しい","普通","悲しい"]
				},
				{
					q:"ご協力ありがとうございました。",
					a:[]
				}
			]
		};
		//var j = JSON.encode(jsonobj);
		//trace(j);
		//trace(JSON.decode(j).scenes);

		scene = new SceneManager();
		for ( i in 0...jsonobj.scenes.length-1 ) {
			var s = jsonobj.scenes[i];
			scene.addNextHandler(create(s.q,s.a),true);
		}
		var s = jsonobj.scenes[jsonobj.scenes.length-1];
		scene.addNextHandler(create(s.q,s.a));
		scene.addNextHandler(end);
		scene.begin();
	}

	// }}}
	// {{{ public function create( q:String, a:Array<String> ) : Void

	public function create( q:String, a:Array<String> ) : Void->Void
	{
		var me = this;
		return function() {
			//trace("scene ... begin");
			if ( me.s!=null ) UXRoot.removeUXChild(me.s);
			me.s = new UXBasicComponent();
			me.s.ctrl.getInteraction("Final").setActSequentialMode(true);
			me.s.ctrl.getInteraction("Final").addAct(me.va.fade(me.fadeparam));
			me.s.ctrl.getInteraction("Final").addFinishHandler(function(s){ me.scene.next(); });
			var label = new UXLabel(q,20);
			var sel  = new UXSelector(EXCLUSIVE);
			var sx = 0;
			var count:Int = 0;
			for ( s in a ) {
				count++;
				var com = new UXItem(null,new UXLabel(s,16),s);
				sel.addUXChild(com);
				com.x = sx;
				com.y = 0;
				sx += 100;
			}
			if ( count>0 ) me.selectors.push( sel );
			me.s.addUXChild(label);
			me.s.addUXChild(sel);
			me.s.x = 200;
			me.s.y = 100;
			label.x = 0;
			label.y = -0;
			UXRoot.addUXChild(me.s);
			sel.ctrl.getInteraction("Select").addFinishHandler(function(state){
				me.s.ctrl.trigger("Final");
			});
			//trace("scene ... end");
		};
	}

	// }}}
	// {{{ public function end() : Void

	public function end() : Void
	{
		formdata.getValues();
		trace("end");
	}

	// }}}
	// {{{ public function getData() : Hash<String>

	public function getData() : Hash<String>
	{
		for ( i in 0...selectors.length ) {
			trace("["+Std.string(i)+"]: " + selectors[i].getSelectionValues());
		}
		return new Hash();
	}

	// }}}
}
// }}}
